<?php
namespace Crawl\Controller;
use Common\Controller\AdminbaseController;
use Think\Cache\Driver\Redis;
// +----------------------------------------------------------------------
// | Date:2018年1月19日
// +----------------------------------------------------------------------
// | Author: EK_熊<1439527494@qq.com>
// +----------------------------------------------------------------------
// | Description: 此文件作用于****
// +----------------------------------------------------------------------
class SourceController extends AdminbaseController{
    
    protected $model;
    public function _initialize(){
        $this->model = M('CrawlSource');
        $this->redis_key = C('REDIS_PREFIX')."source";
    }
    
    public function index(){
        
        $list = $this->lists($this->model);
        $this->assign('list',$list);
        $this->display();    
    }
    
    public function flush(){
        $oldkey = $this->model->getField('key',true);
        $redis = new Redis();
        $redis->ltrim($this->redis_key,1,0);
        foreach ($oldkey as $v){
            $redis->lPush($this->redis_key,$v);
        }
        $this->success('缓存更新完成！~');
    }
    
    
    /**
     * 编辑
     *
     * date:2016年11月22日
     * author: EK_熊
     */
    public function edit(){
       self::edit_redis('url');
    }
    
    /**
     * 新增
     *
     * date:2016年11月22日
     * author: EK_熊
     */
    public function add(){
         
        if (IS_POST) {
            $redis = new Redis();
            $post = I('post');
            if (empty($post['name'])) $this->error('请输入名称');
            if (empty($post['url'])) $this->error('请输入链接地址');
            
            $post['update_time'] =CURRENT_TIME;
             
            $exist = $this->model->where(['key'=>$post['key']])->find();
            if ($exist) $this->error('该关键词已经存在');
             
            $ret = $this->model->add($post);
            if (!$ret) $this->error('添加失败！');
             
            $ret_redis = $redis->lPush($this->redis_key,$post['key']);
             
            $this->success('添加成功！');
        }
    
        $this->display();
    }
    
    /**
     * 删除数据,可批量
     * @param unknown $model
     * date:2016年11月22日
     * author: EK_熊
     */
    public function delete($model=''){
        $redis = new Redis();
        $model = $this->model;
        $ids = I('ids');
        $pk = $model->getPk();
        $map[$pk] = array('in',$ids);
         
        $findret = $model->where($map)->getField('key',true);
         
        $ret = $model->where($map)->delete();
        if (!$ret) $this->error('操作失败,网络异常！');
         
        foreach ($findret as $v) {
            $ret_redis = $redis->sRem($this->redis_key,$v);
        }
         
        $this->success('操作成功！');
    }
    
    
    
}